//
//  LauHomeViewController.swift
//  SwiftProject
//
//  Created by Lausen on 2017/7/9.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

fileprivate let cellId = "cellId"

class LauHomeViewController: LauBaseViewController {

    ///懒加载一个视图模型的对象
    fileprivate lazy var listViewModel = LauStatusListViewModel()
    
    //MARK: -- 调用了父类的loadData方法
    override func loadData() {
        
        print("刷新的最后一条数据\(String(describing: listViewModel.listStatus.last?.text))")
        
        listViewModel.statusList(isPullUp: isPullUp) { (isSuccess,shouldRefresh) in
            
            ///结束刷新动作,
            self.refreshControl?.endRefreshing()
            
            ///恢复上拉的开关
            self.isPullUp = false
            
            if shouldRefresh{
                
                ///刷新表格
                self.tableView?.reloadData()
            }
        }
    }
    
    ///左item的点击事件
    @objc fileprivate func showFirends(){
        
        let demoVC = LauDemoViewController()
        
//        ///隐藏tabBar
//        demoVC.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(demoVC, animated: true)
    }
}

//MARK: -- 设置界面
extension LauHomeViewController{
    
    //MARK: -- 设置表格视图
    override func setupTableView() {
        super.setupTableView()

        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFirends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupTitle()
    }
    
    //MARK: -- 添加首页上面导航栏上面的按钮
    func setupTitle() -> Void {
        
        let button = LauTitleButton(title: LauNetworkManager.shared.account.screen_name)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    }
    
    //MARK: -- navItem上的按钮的点击事件
    @objc private func buttonClick(sender: UIButton){
    
        sender.isSelected = !sender.isSelected
    }
}

//MARK: -- tableView的代理方法,通过子类再次实现
extension LauHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.listStatus.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     
        //1:获取可重用cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2:设置cell
        cell.textLabel?.text = listViewModel.listStatus[indexPath.row].text
        
        //3:返回cell
        return cell
    }
}



